import _ from 'lodash'

let rpchook = {
    /**
     * 预处理rpc上送参数
     * @param {Object} rpcParams rpc上送参数
     */
    pre (rpcInfo,requestType, token) {
        let { userInfo } = app.vm.$store.getters;

        let str19 = 'A';
        let date = new Date();
        let str20 = app.tools.createdTimeFormat(date);
        let str21 = app.tools.randomString(40);
        let str = str19 + str20 + str21;

        //具体项目报文组装
        let data = rpcInfo.requestData;
        data.rocket = str;
        rpcInfo.requestData = {
            'head': {
                'mp_sId': '',
                'sysVersion': '',
                'deviceType': '',
                'type': 'J',
                'utdid': '',
                'channel': 'YNET',
                'channelIP': '',
                'transCode': rpcInfo.operationType,
                'transTime': '',
                'transId': '',
                'sysId': 'YNET',
                'rocket': str,
                'authChainKey': token
            },
            'body': {
                ...data
            }
        }

        !rpcInfo.options && (rpcInfo.options = {});
        !rpcInfo.options.headers && (rpcInfo.options.headers = {});

        //设置cookie
        if (userInfo.sessionID) {
            rpcInfo.options.headers.Cookie = 'JSESSIONID=' + userInfo.sessionID;
        }
        rpcInfo.options.headers.type = 'J';
        if(requestType ==='rpc'){
            //只有走rpc请求，才需要拼接请求接口交易码
            rpcInfo.operationType = RPC_PATH + rpcInfo.operationType;
        }
    },
    // /**
    //  * 预处理HttpRequest上送参数
    //  * @param {Object} rpcParams rpc上送参数
    //  */
    // preHttpRequest (rpcInfo, token) {
    //     let { userInfo } = app.vm.$store.getters;

    //     let str19 = 'A';
    //     let date = new Date();
    //     let str20 = app.tools.createdTimeFormat(date);
    //     let str21 = app.tools.randomString(40);
    //     let str = str19 + str20 + str21;

    //     //具体项目报文组装
    //     let data = rpcInfo.requestData;
    //     data.rocket = str;
    //     console.log(token, '3434')
    //     rpcInfo.requestData = {
    //         'head': {
    //             'mp_sId': '',
    //             'sysVersion': '',
    //             'deviceType': '',
    //             'type': 'J',
    //             'utdid': '',
    //             'channel': 'YNET',
    //             'channelIP': '',
    //             'transCode': rpcInfo.operationType,
    //             'transTime': '',
    //             'transId': '',
    //             'sysId': 'YNET',
    //             'rocket': str,
    //             'authChainKey': token
    //         },
    //         'body': {
    //             ...data
    //         }
    //     }

    //     !rpcInfo.options && (rpcInfo.options = {});
    //     !rpcInfo.options.headers && (rpcInfo.options.headers = {});

    //     //设置cookie
    //     if (userInfo.sessionID) {
    //         rpcInfo.options.headers.Cookie = 'JSESSIONID=' + userInfo.sessionID;
    //     }
    //     rpcInfo.options.headers.type = 'J';
    //     //rpcInfo.operationType = RPC_PATH + rpcInfo.operationType;
    //},
    /**
    * 后处理rpc结果信息
    * @param {Object} res 结果信息对象
    */
    post ({ api, resolve, reject, rpcInfo, result, type }) {
        console.log(`type:${type}`);
        let { options = {} } = rpcInfo;
        let processError = options.processError;
        console.log('===返回报文1===');
        console.log(result);
        console.log('===返回报文2===');
        if (result.error === '0000') {
            console.log(result.data,"oooooooo");
            if (result.data) {
                console.log('result.data');
                //let resData = JSON.parse(result.data);
                let resData = result.data;
                //let header = resData.header;
                //let body = resData.body;
                if (resData.body) {
                    console.log('resData有body');
                    // if (resData.errorCode === '0000') {
                        return resolve(resData.body);
                    // } else {
                    //     return reject(new RpcError(result));
                    // }
                } else {
                    console.log('resData没有body');
                    // if (resData.errorCode === '0000') {
                        return resolve(resData);
                    // } else {
                    //     return reject(new RpcError(result));
                    // }
                }
            } else {
                return reject(new RpcError(result))
            }
        } else {
            if (result.error === 'CMS12907') {
                if (window.loginOut) {
                    return;
                }
                window.loginOut = true;
                //单点登录
                api.alert('您的账号在其他设备登录，请重新登录', '您已被迫下线').then(() => {
                    window.loginOut = null;
                    api.loginOut();
                });
            } else if (result.error === 'CMS11017') {
                return reject(new RpcError(result));
            }
            if (processError && _.isFunction(processError)) {
                processError(result)
            } else if (processError !== false) {
                return reject(new RpcError(result));
            } else {
                console.log('@@@@@@', reject(new RpcError(result)))
                return reject(new RpcError(result));
            }
        }
    }
}

function RpcError (res) {
    console.log(res, '22222222222222222')
    let resData = ''
    let showMsg = '';
    if (res.data) {
        resData = JSON.parse(res.data);
        console.log(res, 'rrrrrrrrrrrr')
        this.header = resData.head;
        this.body = resData.body;
        this.errorCode = res.error;
        this.errorMsg = decodeURI(res.errorMessage + '');
        if (this.errorMsg.length === 0) {
            this.errorMsg = '错误码：' + this.errorCode +
                ' \n 交易失败';
        }
        if (this.header) {
            if (process.env.NODE_ENV !== 'production') {
                showMsg = this.errorMsg;
            } else {
                showMsg = this.errorMsg.replace(/^\d+[:：]/, '');
            }
        }
    } else if (res.error !== undefined && res.error !== '') {
        showMsg = res.errorMessage ? res.errorMessage : '交易异常，请以实际结果为准';
    } else {
        showMsg = '网络请求失败，请重试！';
    }
    console.log(res, showMsg)
    this.response = res;
    this.showMsg = showMsg;
}

export default rpchook;
